import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.UnknownHostException;

import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;


public class ClientManager {
	
	private NetworkHandler networkHandler; 
	private NetworkInput networkInput;
	private imageUpdate imgUpdate;
	private Images images;
	private ClientGUI clientGUI; 
	private XMLHandler outXML;
	
	public ClientManager() throws IOException
	{
		outXML = new XMLHandler(); 
		images = new Images();
		clientGUI = new ClientGUI(images);
		clientGUI.addActionListener(new ClientButtonsActionListener());
	}
	
	void setupNetwork(String host, String port, String robotname) throws UnknownHostException, IOException
	{
		networkHandler = new NetworkHandler(host, port, robotname);
		networkInput = new NetworkInput(networkHandler, images, clientGUI);
		
		//start thread that listens for input
		networkInput.start();
		
	}
	
	
	//Class which listens for buttons in GUI 
	class ClientButtonsActionListener implements ActionListener
	{
		
		@Override
		public void actionPerformed(ActionEvent event) 
		{
			if(event.getSource() == clientGUI.getForwardButton())
			{
				//forward
				try
				{
					if(clientGUI.getNumberInput().equals("")) //If integer input field is empty, just send forward message
					{
						//TODO: send forward message
						JOptionPane.showMessageDialog (null, "Enter a value", "Alert!", JOptionPane.INFORMATION_MESSAGE);
					}
					else 
					{
						outXML.addCommand(new ServerCommand("move", new CommandParameter("req",  clientGUI.getInputValue())));
						networkHandler.send(outXML.toString("s2r")+"\n");
						outXML.clear();

						//TODO: Send forward message with specified distance 
						//System.out.println(Integer.parseInt(clientGUI.getNumberInput()));
					}
				}
				catch(NumberFormatException e)
				{
					//clientGUI.displayErrorMessage("Can only take integers");
				}
			}
			else if(event.getSource() == clientGUI.getBackwardButton())
			{
				//backward
				try
				{
					if(clientGUI.getNumberInput().equals("")) //If integer input field is empty, just send backward message
					{
						//TODO: Send backward message
						JOptionPane.showMessageDialog (null, "Enter a value", "Alert!", JOptionPane.INFORMATION_MESSAGE);
					}
					else 
					{
						//TODO: Send backward message with specified distance 
						outXML.clear();
						outXML.addCommand(new ServerCommand("move", new CommandParameter("req", -clientGUI.getInputValue())));
						networkHandler.send(outXML.toString("s2r")+"\n");
						outXML.clear();
					}
					
				}
				catch(NumberFormatException e)
				{
					//clientGUI.displayErrorMessage("Can only take integers");
				}
			}
			else if(event.getSource() == clientGUI.getTurnLeftButton())
			{
				//turn left (should it only be able to turn 90 degrees? )
				try
				{
					if(clientGUI.getNumberInput().equals("")) 
					{
						JOptionPane.showMessageDialog (null, "Enter a value", "Alert!", JOptionPane.INFORMATION_MESSAGE);
					}
					else 
					{
						outXML.clear();
						outXML.addCommand(new ServerCommand("rotate", new CommandParameter("req", -clientGUI.getInputValue())));
						networkHandler.send(outXML.toString("s2r")+"\n");
						outXML.clear();
					}
				}catch(NumberFormatException e) {
					//Display error here
				}
			}
				
			else if(event.getSource() == clientGUI.getTurnRightButton())
			{
				//turn right (should it only be able to turn 90 degrees? )
				try
				{
					if(clientGUI.getNumberInput().equals("")) 
					{
						JOptionPane.showMessageDialog (null, "Enter a value", "Alert!", JOptionPane.INFORMATION_MESSAGE);
					}
					else 
					{
						outXML.clear();
						outXML.addCommand(new ServerCommand("rotate", new CommandParameter("req", clientGUI.getInputValue())));
						networkHandler.send(outXML.toString("s2r")+"\n");
						System.out.println(outXML.toString("s2r")+"\n");
						outXML.clear();
					}
				}catch(NumberFormatException e) {
					//Display error here
				}
			}
			
			else if(event.getSource() == clientGUI.getScanButton())
			{
				System.out.println("SCAN!");
				//turn right (should it only be able to turn 90 degrees? )
				try
				{
					if(clientGUI.getNumberInput().equals("")) 
					{
						JOptionPane.showMessageDialog (null, "Enter a value", "Alert!", JOptionPane.INFORMATION_MESSAGE);
					}
					else 
					{
						outXML.clear();
						outXML.addCommand(new ServerCommand("scan", new CommandParameter("req", clientGUI.getInputValue())));
						networkHandler.send(outXML.toString("s2r")+"\n");
						outXML.clear();
					}
				}catch(NumberFormatException e) {
					//Display error here
					JOptionPane.showMessageDialog (null, "number format", "Alert!", JOptionPane.INFORMATION_MESSAGE);
				}
			}
			
			else if(event.getSource() == clientGUI.getAutoButton())
			{
				//auto mode, var det G2AutoMode som taggen hette ?
				
				outXML.clear();
				outXML.addCommand(new ServerCommand("G2autoMode", new CommandParameter("req",  clientGUI.getAutoStatus() )));
				networkHandler.send(outXML.toString("s2r")+"\n");
				outXML.clear();
				
				
			}
			
		}
		
	}
	
	public static void main(String [] args) throws IOException
	{
		ClientManager cm = new ClientManager();
		cm.clientGUI.startScreen();
		cm.setupNetwork(cm.clientGUI.getHost(), cm.clientGUI.getPort(), cm.clientGUI.getRobotName());
		//cm.clientGUI.setVisible(true);
	}
}
